﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class EditProduct : System.Web.UI.UserControl
    {
        public string image;
        public string name;
        public string discription;
        public string price;
        public string discount;
        public string summary;
        public string cate1_id;
        public string cate2_id;
        public string cate3_id;
        public string idcolor;
        public string idsize;
        public string idvendor;
        public string linkbaohanh;
        public string cate1_name;
        public string bigimage;
        public string smallimage;
        public int idproduct;
        public string imagesmall1;
        public string imagesmall2;
        public string imagesmall3;
        public LIB.Validate saveimage;
        public string filePathBigIamge;
        public string filePathSmallIamge;
        public string activesize;
        public string activediscount;
        public string activelinkbaohanh;
        public string activespch;
        protected void Page_Load(object sender, EventArgs e)
        {

            idproduct = Convert.ToInt32(Request.QueryString["idproduct"]);
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Product", "*", "Product_ID", idproduct, "selectwhereid");
            cate1_id = dt.Rows[0]["Category_ID1"].ToString();
            cate2_id = dt.Rows[0]["Category_ID2"].ToString();
            cate3_id = dt.Rows[0]["Category_ID3"].ToString();
            idvendor = dt.Rows[0]["Vendor_ID"].ToString();
            image = dt.Rows[0]["Product_Image"].ToString();
            name = dt.Rows[0]["Product_Name"].ToString();
            discription = dt.Rows[0]["Product_Descriptions"].ToString();
            price = dt.Rows[0]["Product_SalesPrice"].ToString();
            discount = dt.Rows[0]["Product_Discount"].ToString();
            summary = dt.Rows[0]["Product_Summary"].ToString();
            linkbaohanh = dt.Rows[0]["Product_LinkWarranty"].ToString();
            activesize = dt.Rows[0]["Product_ViewSize"].ToString();
            activediscount = dt.Rows[0]["Product_ViewDiscount"].ToString();
            activelinkbaohanh = dt.Rows[0]["Product_ViewWarranty"].ToString();
            activespch = dt.Rows[0]["Product_Spch"].ToString();
            txtProductName.Text = name;
            txtPrice.Text = price;
            txtDiscount.Text = discount;
            CKEditorMota.Text = summary;
            CKEditorChitiet.Text = discription;
            txtBaohanh.Text = linkbaohanh;


            if (!Page.IsPostBack)
            {
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", 0, "selectwhereid");
                ListItem lt1 = new ListItem("Chon danh muc", "-1");
                ddlCategory1.Items.Add(lt1);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt1 = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlCategory1.Items.Add(lt1);
                }
                for (int i = 0; i < ddlCategory1.Items.Count; i++)
                {
                    if (ddlCategory1.Items[i].Value == cate1_id)
                    {
                        ddlCategory1.Items[i].Selected = true;
                    }
                }
                //Load du lieu
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", cate1_id, "selectwhereid");
                ListItem lt2 = new ListItem("Chon danh muc", "-1");
                ddlCategory2.Items.Add(lt2);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt2 = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlCategory2.Items.Add(lt2);
                }
                for (int i = 0; i < ddlCategory2.Items.Count; i++)
                {
                    if (ddlCategory2.Items[i].Value == cate2_id)
                    {
                        ddlCategory2.Items[i].Selected = true;

                    }
                }
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", cate2_id, "selectwhereid");
                ListItem lt3 = new ListItem("Chon danh muc", "-1");
                ddlCategory3.Items.Add(lt3);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt3 = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlCategory3.Items.Add(lt3);
                }
                for (int i = 0; i < ddlCategory3.Items.Count; i++)
                {
                    if (ddlCategory3.Items[i].Value == cate3_id)
                    {
                        ddlCategory3.Items[i].Selected = true;

                    }
                }

                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectByColumns("tbl_Vendor", " * ", "selectbycolumns");

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem ltvendor = new ListItem(dt.Rows[i]["Vendor_Name"].ToString(), dt.Rows[i]["Vendor_ID"].ToString());
                    ddl_Vendor.Items.Add(ltvendor);
                }
                for (int i = 0; i < ddl_Vendor.Items.Count; i++)
                {
                    if (ddl_Vendor.Items[i].Value == idvendor)
                    {
                        ddl_Vendor.Items[i].Selected = true;
                    }
                }

                if (activesize == "")
                {
                    cbxSize.Checked = true;
                }
                else
                {
                    cbxSize.Checked = false;
                }
                if (activediscount == "")
                {
                    cbxactivediscount.Checked = true;
                }
                else
                {
                    cbxactivediscount.Checked = false;
                }
                if (activelinkbaohanh == "")
                {
                    cbxlinkbaohanh.Checked = true;
                }
                else
                {
                    cbxlinkbaohanh.Checked = false;
                }
                if (activespch == "")
                {
                    cbxspch.Checked = true;
                }
                else
                {
                    cbxspch.Checked = false;
                }
            }
        }


        protected void btnUpdateProduct_Click(object sender, EventArgs e)
        {
            int idproduct = Convert.ToInt32(Request.QueryString["idproduct"]);
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Product", "*", "Product_ID", idproduct, "selectwhereid");
            bigimage = dt.Rows[0]["Product_Image"].ToString();
            int vendor_id = Convert.ToInt32(ddl_Vendor.SelectedValue.ToString());
            string productname = txtProductName.Text;
            string summary = CKEditorMota.Text;
            string description = CKEditorChitiet.Text;
            float price = float.Parse(txtPrice.Text);
            float disconut = float.Parse(txtDiscount.Text);
            string linkbaohanh = txtBaohanh.Text;


            if (productname.Equals(""))
            {
                Response.Write("<script>alert('Vui lòng nhập tên sản phẩm...')</script>");
            }
            else
            {
                if (price.Equals(""))
                {
                    Response.Write("<script>alert('Vui lòng nhập giá sản phẩm...')</script>");
                }
                else
                {
                    if (disconut.Equals(""))
                    {
                        Response.Write("<script>alert('Vui lòng nhập giảm giá sản phẩm...')</script>");
                    }
                    else
                    {
                        int cate1_index = ddlCategory1.SelectedIndex;

                        int cate2_index = ddlCategory2.SelectedIndex;
                        int cate3_index = ddlCategory3.SelectedIndex;

                        if (cate1_index > 0)
                        {
                            int cate1_id = Convert.ToInt32(ddlCategory1.SelectedValue.ToString());

                            int cate2_id = 0;
                            int cate3_id = 0;


                            if (cate2_index < 1)
                            {
                                cate2_id = cate1_id;
                            }
                            else
                            {
                                cate2_id = Convert.ToInt32(ddlCategory2.SelectedValue.ToString());
                            }
                            if (cate3_index < 1)
                            {
                                cate3_id = cate2_id;
                            }
                            else { cate3_id = Convert.ToInt32(ddlCategory3.SelectedValue.ToString()); }
                            if (cbxSize.Checked)
                            {
                                activesize = "";
                            }
                            else
                            {
                                activesize = "hidden";
                            }

                            if (cbxactivediscount.Checked)
                            {
                                activediscount = "";
                            }
                            else
                            {
                                activediscount = "hidden";
                            }
                            if (cbxlinkbaohanh.Checked)
                            {
                                activelinkbaohanh = "";
                            }
                            else
                            {
                                activelinkbaohanh = "hidden";
                            }
                            if (cbxspch.Checked)
                            {
                                activespch = "";
                            }
                            else
                            {
                                activespch = "hidden";
                            }
                            if (fulImage.HasFile)
                            {
                                if (LIB.Validate.CheckFileType(fulImage.FileName))
                                {
                                    saveimage = new LIB.Validate();
                                    filePathBigIamge = "/Images/Image-Products/" + idproduct + "a" + Path.GetExtension(fulImage.FileName) + "";
                                    saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Products/"), idproduct + "a" + Path.GetExtension(fulImage.FileName), 500, fulImage.PostedFile.InputStream);
                                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[15];
                                    cl[0] = new DAL.DHelpers.Column("Category_ID1", cate1_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[1] = new DAL.DHelpers.Column("Category_ID2", cate2_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[2] = new DAL.DHelpers.Column("Category_ID3", cate3_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                    cl[3] = new DAL.DHelpers.Column("Product_Name", productname, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[4] = new DAL.DHelpers.Column("Vendor_ID", vendor_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[5] = new DAL.DHelpers.Column("Product_Image", filePathBigIamge, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[6] = new DAL.DHelpers.Column("Product_Summary", summary, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[7] = new DAL.DHelpers.Column("Product_Descriptions", description, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[8] = new DAL.DHelpers.Column("Product_SalesPrice", price.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[9] = new DAL.DHelpers.Column("Product_discount", disconut.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[10] = new DAL.DHelpers.Column("Product_ViewDiscount", activediscount, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[11] = new DAL.DHelpers.Column("Product_ViewSize", activesize, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[12] = new DAL.DHelpers.Column("Product_LinkWarranty", linkbaohanh, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[13] = new DAL.DHelpers.Column("Product_ViewWarranty", activelinkbaohanh, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                    cl[14] = new DAL.DHelpers.Column("Product_Spch", activespch, DAL.DHelpers.DataType.DataTypeSql.Varchar);

                                    string[] colWhere = new string[1];
                                    colWhere[0] = "Product_ID";

                                    object[] colValue = new object[1];
                                    colValue[0] = idproduct;

                                    DAL.DCollector.DBCollector.Instance.Update("tbl_Product", cl, colWhere, colValue);
                                    Response.Redirect("/Admin/Index.aspx?panel=1&sub=5&idproduct=" + idproduct);
                                }
                                else
                                {
                                    Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                                }
                            }
                            else
                            {
                                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[14];
                                cl[0] = new DAL.DHelpers.Column("Category_ID1", cate1_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[1] = new DAL.DHelpers.Column("Category_ID2", cate2_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[2] = new DAL.DHelpers.Column("Category_ID3", cate3_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[3] = new DAL.DHelpers.Column("Product_Name", productname, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[4] = new DAL.DHelpers.Column("Vendor_ID", vendor_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[5] = new DAL.DHelpers.Column("Product_Summary", summary, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[6] = new DAL.DHelpers.Column("Product_Descriptions", description, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[7] = new DAL.DHelpers.Column("Product_SalesPrice", price.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[8] = new DAL.DHelpers.Column("Product_discount", disconut.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[9] = new DAL.DHelpers.Column("Product_ViewDiscount", activediscount, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[10] = new DAL.DHelpers.Column("Product_ViewSize", activesize, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[11] = new DAL.DHelpers.Column("Product_LinkWarranty", linkbaohanh, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[12] = new DAL.DHelpers.Column("Product_ViewWarranty", activelinkbaohanh, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[13] = new DAL.DHelpers.Column("Product_Spch", activespch, DAL.DHelpers.DataType.DataTypeSql.Varchar);

                                string[] colWhere = new string[1];
                                colWhere[0] = "Product_ID";

                                object[] colValue = new object[1];
                                colValue[0] = idproduct;

                                DAL.DCollector.DBCollector.Instance.Update("tbl_Product", cl, colWhere, colValue);
                                Response.Redirect("/Admin/Index.aspx?panel=1&sub=5&idproduct=" + idproduct);
                            }
                        }
                    }
                }
            }
        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Chon danh muc cap 1 load danh muc cap 2
                ddlCategory2.Items.Clear();
                ddlCategory3.Items.Clear();

                int index = ddlCategory1.SelectedIndex;
                if (index > 0)
                {
                    int id = Convert.ToInt32(ddlCategory1.SelectedValue);
                    // Load du lieu
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
                    ListItem lt = new ListItem("Chon danh muc", "-1");
                    ddlCategory2.Items.Add(lt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                        ddlCategory2.Items.Add(lt);
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Chon danh muc cap 1 load danh muc cap 2
                ddlCategory3.Items.Clear();
                int index = ddlCategory2.SelectedIndex;
                if (index > 0)
                {
                    int id = Convert.ToInt32(ddlCategory2.SelectedValue);
                    // Load du lieu
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
                    ListItem lt = new ListItem("Chon danh muc", "-1");
                    ddlCategory3.Items.Add(lt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                        ddlCategory3.Items.Add(lt);
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }
    }
}